﻿using DevExpress.Spreadsheet;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.nlp.service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.nlp
{
    public partial class FormExcel显示 : DevExpress.XtraEditors.XtraForm
    {
        public FormExcel显示()
        {
            InitializeComponent();
        }

        private void simpleButton根据关键字检索json_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(this.textBox根据关键字检索.Text))
            {
                //this.showJsonToExcel(this.textBox根据关键字检索.Text.Trim());
                //读json文件取JSON串
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "请选择JSON文件";
                ofd.Filter = "(*.json)|*.json";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    String strJson = FileUtil.GetJsonFile(ofd.FileName);
                    this.JsonToExcel(strJson);
                }
            }
        }

        private void showJsonToExcel(String keyword)
        {
            //  根据关键字检索json
            String strJson = NlpService.getJSONByKeyword(keyword);
            //  json转excel
            String saveDirectoryPath = @"doc\";
            String fileName = saveDirectoryPath + DateTime.Now.ToString("yyyyMMdd") +  "json转Excel.xls";
            FileUtil.JsonToDT(fileName, strJson);
            //  打开excel
            if (!string.IsNullOrEmpty(fileName))
            {
                IWorkbook workbook1 = spreadsheetControl1.Document;
                workbook1.LoadDocument(fileName);
            }
        }

        private void JsonToExcel(String strJson)
        {
            //  json转excel
            String saveDirectoryPath = @"doc\";
            String fileName = saveDirectoryPath + "json转Excel.xls";
            FileUtil.JsonToDT(fileName, strJson);
            //  打开excel
            if (!string.IsNullOrEmpty(fileName))
            {
                IWorkbook workbook1 = spreadsheetControl1.Document;
                workbook1.LoadDocument(fileName);
            }
        }
    }
}